/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.base.thread
 * author:David
 * Date:2015年8月20日下午4:56:00
 * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.base.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author David
 * @since 2015年8月20日 下午4:56:00
 * @version 
 * @since JDK 1.6
 */
public class CacheDemo {

	private Map<String ,Object> cache=new HashMap<String ,Object>();
	
	public static void main(String[] args) {

		
	}
	
	private ReadWriteLock rwl=new ReentrantReadWriteLock();
	public Object getData(String key){
		rwl.readLock().lock();
		Object value=null;
		try {
			value = cache.get(key);
			if (value == null) {
				rwl.readLock().unlock();
				rwl.writeLock().lock();
				try{
					if(value==null){
						value = "aaa";//去查数据库
					}
				}finally{
					rwl.writeLock().unlock();
				}
				rwl.readLock().lock();
			}
		} finally{
			rwl.readLock().unlock();
		}
		return value;
		
	}

}
